package com.example.cupid.service.Impl;

import com.example.cupid.mapper.WechatMapper;
import com.example.cupid.pojo.UserWechatSava;
import com.example.cupid.pojo.UserWechatTake;
import com.example.cupid.pojo.Wechat;
import com.example.cupid.service.WechatService;
import com.example.cupid.util.MsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 2021/9/3
 **/
@Service
public class WechatServiceImpl implements WechatService {

    @Autowired
    private WechatMapper wechatMapper;

    @Override
    public List<Wechat> getSaveWechatByUserId(int id) { return wechatMapper.selectSaveWechatByUserId(id); }

    @Override
    public List<Wechat> getTakeWechatByUserId(int id) {
        return wechatMapper.selectTakeWechatByUserId(id);
    }

    @Override
    public List<Wechat> getSaveWechatBySearch(int sex) { return wechatMapper.selectSaveWechatBySearch(sex); }

    @Override
    public List<UserWechatSava> getSaveWechat() {
        return wechatMapper.selectUserWechatSava();
    }

    @Override
    public List<UserWechatTake> getTakeWechat() {
        return wechatMapper.selectUserWechatTake();
    }

    @Override
    public boolean saveWechat(Wechat wechat) {
        return wechatMapper.insertWechat(wechat);
    }

    @Override
    public int getWechatIdByWechatName(String wechat) {
        return wechatMapper.selectWechatIdByWechatName(wechat);
    }

    @Override
    public boolean saveUserWechatSave(Integer userId, Integer wechatId) {
        return wechatMapper.insertUserWechatSave(userId,wechatId);
    }

    @Override
    public boolean saveUserWechatTake(Integer userId, Integer wechatId) {
        return wechatMapper.insertUserWechatTake(userId,wechatId);
    }

    @Override
    public Wechat getWechat(String wechat) {
        return wechatMapper.selectWechat(wechat);
    }

    @Override
    public List<String> getTagNamesByWechatId(int id) {
        return wechatMapper.selectTagNamesByWechatId(id);
    }

    @Override
    public int checkWechat(String wechat) {
        return wechatMapper.checkWechat(wechat);
    }

    @Override
    public List<Wechat> getWechatsAccordWithUser(Wechat wechat,Integer userId) {
        List<Integer> exclude = getExcludeWechatId(userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("wechat",wechat);
        map.put("exclude",exclude);
        List<Wechat> wechats = wechatMapper.selectWechatsAccordWithUser(map);
        for (Wechat wechat1 : wechats) {
            List<String> strings = wechatMapper.selectTagNamesByWechatId(wechat1.getId());
            wechat1.setTagNames(strings);
        }
        return wechats;
    }
    @Override
    public List<UserWechatSava> getSaveWechat() {
        return wechatMapper.selectUserWechatSava();
    }

    @Override
    public List<UserWechatTake> getTakeWechat() {
        return wechatMapper.selectUserWechatTake();
    }

    @Override
    public Wechat getRandomWechatBySex(int userId, int sex) {
        List<Integer> exclude = getExcludeWechatId(userId);
        Wechat wechat = wechatMapper.selectRandomWechatBySex(sex, exclude);
        List<String> tagNames = wechatMapper.selectTagNamesByWechatId(wechat.getId());
        wechat.setTagNames(tagNames);
        System.out.println(wechat);
        return wechat;
    }

    @Override
    public int signalCheck(int userId, Wechat wechat) {
        List<Integer> exclude = getExcludeWechatId(userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("wechat",wechat);
        map.put("exclude",exclude);
        return wechatMapper.selectAccordWithSignalCount(map);
    }
    private List<Integer> getExcludeWechatId(int userId){
        List<Integer> alreadyTakeWechatsId = wechatMapper.selectTakeWechatsIdByUserId(userId);
        List<Integer> alreadySaveWechatsId = wechatMapper.selectSaveWechatsIdByUserId(userId);
        List<Integer> exclude = new ArrayList<>();
        exclude.addAll(alreadySaveWechatsId);
        exclude.addAll(alreadyTakeWechatsId);
        return exclude;
    }
}
